//
//  WXPayRequestHandler.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/5/5.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "WXPayRequestHandler.h"


@implementation WXPayRequestHandler

+ (void)wxPayRequestWithTransRecordId:(NSString *)transRecordId
{
    NSDictionary *body = @{
                           @"transRecordId":transRecordId,
                           @"memberId":UserUtils.shared.vipId
                           };
    [URLManager.shareManager postUrlStr:Api_Pay_Weixin parameters:body success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                NSDictionary *data = responseObj[@"data"];
                NSString *partnerid = data[@"partnerid"];
                NSString *prepayid = data[@"prepayid"];
                NSString *noncestr = data[@"noncestr"];
                NSString *timestamp = data[@"timestamp"];
                NSString *packageValue = data[@"packageValue"];
                NSString *sign = data[@"sign"];
                //调起微信支付
                PayReq* req             = [[PayReq alloc] init];
                req.partnerId           = partnerid;
                req.prepayId            = prepayid;
                req.nonceStr            = noncestr;
                req.timeStamp           = [timestamp intValue];
                req.package             = packageValue;
                req.sign                = sign;
                [WXApi sendReq:req];
            }
            else {
                [MBProgressHUD showError:responseObj[@"message"]];
            }
        }
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

+ (void)wxPayWithTransRecordId:(NSString *)transRecordId
               CallbackSuccess:(void (^)(NSURLResponse * _Nonnull response, id _Nullable responseObj, NSDictionary * _Nullable otherResponse))success
                       failure:(void (^)(NSURLResponse * _Nullable response, NSError * _Nonnull error))failure
{
    NSDictionary *body = @{
                           @"transRecordId":transRecordId,
                           @"memberId":UserUtils.shared.vipId
                           };
    [URLManager.shareManager postUrlStr:Api_Pay_Weixin_Callback parameters:body success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        
        success(response,responseObj,otherResponse);
        
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        failure(response,error);
    }]; 
}

@end
